package com.info.zhiduoduo.core.service.impl;

import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.company.CompanyPayType;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.core.service.CoreOddJobService;
import com.info.zhiduoduo.repository.entity.crowdsource.CrowdsourceProject;
import com.info.zhiduoduo.repository.entity.crowdsource.OddJobMerchantBalance;
import com.info.zhiduoduo.repository.entity.crowdsource.OddJobMerchantFlow;
import com.info.zhiduoduo.repository.entity.crowdsource.OddJobRechargeRecord;
import com.info.zhiduoduo.repository.mapper.crowdsource.OddJobMerchantBalanceMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * @author tianyonghong
 * @create 2023/2/23
 */
@Slf4j
@Service
public class CordOddJobMerchantServiceImpl implements CoreOddJobService {

    @Resource
    OddJobMerchantBalanceMapper oddJobMerchantBalanceMapper;

    //根据充值申请 生成商户流水/修改商户余额
    public SmartResult rechargeMerchantAndCreateFlowByRecharged(OddJobRechargeRecord rechargeRecord) {
        return createOddJobRechargeRecord(
                rechargeRecord.getMerchantId(),
                rechargeRecord.getRechargePartyId(),
                rechargeRecord.getId(),
                rechargeRecord.getFlowNo(),
                rechargeRecord.getRechargeType(),
                rechargeRecord.getRechargeAmount());
    }

    //根据智零工发布的项目 生成商户流水/修改商户余额
    public SmartResult rechargeMerchantAndCreateFlowByProjected(CrowdsourceProject crowdsourceProject) {

        BigDecimal applyAmount = crowdsourceProject.getProjectTotalAmount()
                                        .add(crowdsourceProject.getProjectTotalAmount()
                                                    .multiply(crowdsourceProject.getTaxSourceCostRate()));
        //固定的河南商户主体(表zdd_odd_job_merchant)=1
        Integer merchantId = 1;
        if (crowdsourceProject.getPayId().equals(CompanyPayType.PINGAN_CHENSHUANG)) {
            merchantId = 2;
        }
        return createOddJobRechargeRecord(merchantId,
                crowdsourceProject.getPartyId(),
                crowdsourceProject.getProjectid(),
                RandomUtil.createRandomNo("ZLG", ""),
                3,
                applyAmount);
    }


    //创建充值记录，修改商户余额
    public SmartResult createOddJobRechargeRecord(Integer merchantId, Integer partyId, Integer sourceId, String flowNo,
                                                  Integer flowType, BigDecimal flowAmount) {

        //查询主体之前的金额
        BigDecimal beforeBalance = BigDecimal.ZERO;
        OddJobMerchantBalance oddJobMerchantBalance = oddJobMerchantBalanceMapper.selectBalanceByMerchantAndParty(merchantId, partyId);
        if (oddJobMerchantBalance == null) {
            oddJobMerchantBalance = new OddJobMerchantBalance();
            oddJobMerchantBalance.setMerchantId(merchantId);
            oddJobMerchantBalance.setPartyId(partyId);
            oddJobMerchantBalance.setBalance(BigDecimal.ZERO);
            oddJobMerchantBalance.insert();
        } else {
            beforeBalance = oddJobMerchantBalance.getBalance();
        }
        //主体现在的金额
        BigDecimal afterBalance = flowType == 3 ? beforeBalance.subtract(flowAmount) : beforeBalance.add(flowAmount);
        //修改金额
        oddJobMerchantBalance.setBalance(afterBalance).updateById();

        //新增流水
        Boolean result = new OddJobMerchantFlow()
                .setFlowNo(flowNo)
                .setMerchantId(merchantId)
                .setPartyId(partyId)
                .setFlowAmount(flowAmount)
                .setFlowType(flowType)
                .setBeforeBalance(beforeBalance)
                .setCurrBalance(afterBalance)
                .setSourceId(sourceId)
                .insert();
        if (!result) {
            return SmartResult.fail();
        }
        return SmartResult.success();
    }
}
